<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图 -->
    <el-card class="box-card">
      <el-button type="primary">添加角色</el-button>
      <!-- 角色列表表格 -->
      <el-table :data="roleslist" border stripe>
        <el-table-column type="expand">
          <template slot-scope="props">
            <!-- 循环生成一级权限 -->
            <el-row
              :class="['bdbottom', i1 === 0 ? 'bdtop' : '','vconter']"
              v-for="(item1,i1) in props.row.children"
              :key="item1.id"
            >
              <!-- 展示一级权限 -->
              <el-col :span="6">
                <el-tag>{{item1.authName}}</el-tag>
                <i class="el-icon-caret-right"></i>
              </el-col>
              <!-- 嵌套二级和三级权限 -->
              <el-col :span="18">
                <!-- 循环 渲染二级权限 -->
                <el-row
                  :class="[i2 === 0 ? '' : 'bdtop','vconter']"
                  v-for="(item2,i2) in item1.children"
                  :key="item2.id"
                >
                  <!-- 展示二级权限 -->
                  <el-col :span="6">
                    <el-tag type="success">{{item2.authName}}</el-tag>
                    <i class="el-icon-caret-right"></i>
                  </el-col>
                  <!-- 展示三级权限 -->
                  <el-col :span="18">
                    <el-tag
                      type="warning"
                      v-for="item3 in item2.children"
                      :key="item3.id"
                      closable
                      @close="removeRightBId(props.row,item3.id)"
                    >{{item3.authName}}</el-tag>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-table-column>
        <el-table-column prop="roleName" label="角色名称"></el-table-column>
        <el-table-column prop="roleDesc" label="角色描述"></el-table-column>
        <el-table-column label="操作" width="300px">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" size="mini">编辑</el-button>
            <el-button type="warning" icon="el-icon-delete" size="mini">删除</el-button>
            <el-button
              @click="getRightsList(scope.row)"
              type="danger"
              icon="el-icon-setting"
              size="mini"
            >分配权限</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 分配权限 -->
    <el-dialog
      title="分配权限"
      :visible.sync="setRightsDialogVisible"
      width="50%"
      @close="setRightsClose()"
    >
      <!-- 树形控件 -->
      <el-tree
        :data="rightsList"
        :default-checked-keys="defaultKeys"
        :props="treeProps"
        show-checkbox
        node-key="id"
        default-expand-all
        ref="tree"
      ></el-tree>
      <span slot="footer" class="dialog-footer">
        <el-button @click="setRightsDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="setRights">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data () {
    return {
      // 所有角色列表数据
      roleslist: [],
      // 控制分配权限对话框的显示与隐藏
      setRightsDialogVisible: false,
      // 所有权限的数据
      rightsList: [],
      // 树形控件的属性绑定对象
      treeProps: {
        children: 'children',
        // 配置将数据每一项哪个字段
        label: 'authName'
      },
      // 设置默认选中
      defaultKeys: [],
      roleId: 0
    }
  },

  created () {
    this.getRolesList()
  },

  methods: {
    // 获取角色列表数据
    async getRolesList () {
      const { data: res } = await this.$http.get('roles')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.roleslist = res.data
    },
    removeRightBId (row, id) {
      this.$confirm('此操作将永久删除该权限, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        // 点击确认
        const { data: res } = await this.$http.delete(`roles/${row.id}/rights/${id}`)
        if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
        row.children = res.data
      }).catch(() => {
        // 点击取消
        this.$message.info('已取消删除')
      })
    },
    // 获取权限列表数据  显示权限分配列表
    async getRightsList (row) {
      this.roleId = row.id
      const { data: res } = await this.$http.get('rights/tree')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.rightsList = res.data
      this.getAllkeys(row, this.defaultKeys)
      this.setRightsDialogVisible = true
    },
    // 递归children
    getAllkeys (row, arr) {
      if (!row.children) {
        return arr.push(row.id)
      }
      row.children.forEach(element => {
        this.getAllkeys(element, arr)
      })
    },
    // 监听关闭分配权限对话框
    setRightsClose () {
      this.defaultKeys = []
    },
    // 给用户分配权限 更新权限
    async setRights () {
      const allKeys = [...this.$refs.tree.getCheckedKeys(), ...this.$refs.tree.getHalfCheckedKeys()]
      const idStr = allKeys.join(',')
      const { data: res } = await this.$http.post(`roles/${this.roleId}/rights`, {
        rids: idStr
      })
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.$message.success(res.meta.msg)
      this.setRightsDialogVisible = false
      this.getRolesList()
    }
  }
}
</script>

<style scoped lang='less'>
.el-button {
  margin-bottom: 14px;
}
.el-tag {
  margin: 7px;
}
.bdtop {
  border-top: 1px solid #eee;
}
.bdbottom {
  border-bottom: 1px solid #eee;
}
.vconter {
  display: flex;
  align-items: center;
}
</style>
